package photos;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import dbTools.DBTools;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * Servlet implementation class PhotoUpdateServlet
 */
@WebServlet("/PhotoUpdateServlet")
public class PhotoUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PhotoUpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		SmartUpload su=new SmartUpload();
		PageContext page=JspFactory.getDefaultFactory().getPageContext(this, request, response, null, true, 10240, true);
		su.initialize(page);
		String photo=null;
		try {
			su.upload();
			int count = su.save("/img/goodsPhotos");
			Files fs=su.getFiles();
			File f=fs.getFile(0);
			photo=f.getFileName();
	        DBTools db=new DBTools();
			db.open();
			String id=su.getRequest().getParameter("id");
			String sql = "UPDATE photoInfo SET photoSrc='"+photo+"' WHERE photoId="+id+";";
			db.update(sql);
			db.close();
		} catch (SmartUploadException e) {
			// TODO: handle exception
		}
        //ȥ��ʾ�ϴ����ļ�
		int i=Integer.parseInt(su.getRequest().getParameter("i"));
		int i2=0;
        DBTools db=new DBTools();
        db.open();
		String sql1="SELECT * FROM photoInfo";
		PhotoInfoMessageBean sc=null;
		ArrayList al1=new ArrayList();
		ResultSet rs1=db.select(sql1);
		try {
			while (rs1.next()) {
				sc=new PhotoInfoMessageBean(rs1.getInt(1),rs1.getString(2));
				al1.add(sc);
			}
			if(al1.size()%5!=0){
				i2=al1.size()/5+1;
				if(i<1){
					i=al1.size()/5+1;
				}
				if(i>al1.size()/5+1){
					i=1;
				}	
			}else{
				i2=al1.size()/5;
				if(i<1){
					i=al1.size()/5;
				}
				if(i>al1.size()/5){
					i=1;
				}	
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql2="SELECT * FROM photoInfo ORDER BY photoId DESC LIMIT "+(i-1)*5+",5;";
		ResultSet rs2=db.select(sql2);
		ArrayList al2=new ArrayList();
		try {
			while (rs2.next()) {
				sc=new PhotoInfoMessageBean(rs2.getInt(1),rs2.getString(2));
				al2.add(sc);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.setAttribute("i", i);
		request.setAttribute("i2", i2);
		request.setAttribute("al", al2);
		db.close();
		request.getRequestDispatcher("/photo.jsp").forward(request, response);
	}

}
